/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.android.receiver.painter;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import com.hercules.android.receiver.Receiver;
import com.hercules.android.receiver.ReceiverUtil;
import com.hercules.android.receiver.entities.XREImage;
import com.hercules.android.receiver.entities.XREView;

/**
 * <p>
 * This class is a painter class which will draw a XREImage resource on a canvas.
 * </p>
 * 
 * @author TCSASSEMBER
 * @version 1.0
 */
public class XREImagePainter extends XREPainter {
    /**
     * Constructor with a <code>Receiver</code> arguments.
     * 
     * @param receiver
     *            the <code>Receiver</code> instance.
     * @throws IllegalArgumentException
     *             if receiver if null
     */
    public XREImagePainter(Receiver receiver) {
        super(receiver);
    }

    /**
     * This method will paint the resource on canvas.
     * 
     * @param view
     *            the resource to paint
     * @param canvas
     *            the canvas that the resource painted on
     * @throws IllegalArgumentException
     *             if arguments is null
     * @throws XREPainterException
     *             if any error occurs
     */
    public void paint(XREView view, Canvas canvas) throws XREPainterException {
        // check arguments
        ReceiverUtil.checkNull("view", view);
        ReceiverUtil.checkNull("canvas", canvas);

        if (view.getResource() == 0) {
            return;
        }
        if (!view.getVisible() || !view.getPainting()) {
            return;
        }

        XREImage image = (XREImage) receiver.getApplicationData().getElementsXREIdToElement().get(view.getResource());

        InputStream is = null;
        try {
            // just download the image from URL at first time
            if (image.getBitmap() == null) {
                // get image from URL
                URL url = new URL(image.getUrl());
                URLConnection connection = url.openConnection();
                is = connection.getInputStream();
                image.setBitmap(BitmapFactory.decodeStream(is));
            }
            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setAlpha(ReceiverUtil.getNormalAlpha(view.getAlpha()));
            canvas.drawBitmap(image.getBitmap(), null,
                    new Rect(0, 0, view.getDimensions()[0], view.getDimensions()[1]), paint);
        } catch (MalformedURLException e) {
            throw new XREPainterException("Image url [" + image.getUrl() + "] can't be resovled.", e);
        } catch (IOException e) {
            throw new XREPainterException("Error occurs when reading image from [" + image.getUrl() + "].", e);
        } finally {
            // close the image resource
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    // ignore
                }
            }
        }
    }
}
